{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 运用微软Azure API识别图片人脸数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'faceId': 'feb7e023-50df-4476-a811-5ec7dfaa731f',\n",
       "  'faceRectangle': {'top': 118, 'left': 144, 'width': 88, 'height': 88},\n",
       "  'faceAttributes': {'gender': 'male',\n",
       "   'age': 18.0,\n",
       "   'glasses': 'NoGlasses',\n",
       "   'emotion': {'anger': 0.0,\n",
       "    'contempt': 0.003,\n",
       "    'disgust': 0.0,\n",
       "    'fear': 0.0,\n",
       "    'happiness': 0.813,\n",
       "    'neutral': 0.184,\n",
       "    'sadness': 0.0,\n",
       "    'surprise': 0.0}}},\n",
       " {'faceId': 'a7ba8baa-b4a3-4349-bd8c-6444e7b1c71e',\n",
       "  'faceRectangle': {'top': 117, 'left': 376, 'width': 64, 'height': 64},\n",
       "  'faceAttributes': {'gender': 'female',\n",
       "   'age': 20.0,\n",
       "   'glasses': 'NoGlasses',\n",
       "   'emotion': {'anger': 0.0,\n",
       "    'contempt': 0.001,\n",
       "    'disgust': 0.0,\n",
       "    'fear': 0.0,\n",
       "    'happiness': 0.456,\n",
       "    'neutral': 0.542,\n",
       "    'sadness': 0.001,\n",
       "    'surprise': 0.0}}},\n",
       " {'faceId': '6ff3abad-c8ed-4336-ae2c-f222d989c3b5',\n",
       "  'faceRectangle': {'top': 41, 'left': 676, 'width': 52, 'height': 52},\n",
       "  'faceAttributes': {'gender': 'male',\n",
       "   'age': 23.0,\n",
       "   'glasses': 'ReadingGlasses',\n",
       "   'emotion': {'anger': 0.0,\n",
       "    'contempt': 0.0,\n",
       "    'disgust': 0.0,\n",
       "    'fear': 0.0,\n",
       "    'happiness': 1.0,\n",
       "    'neutral': 0.0,\n",
       "    'sadness': 0.0,\n",
       "    'surprise': 0.0}}},\n",
       " {'faceId': 'dd6927c2-6766-4bc8-9fe3-b3ee0e42c576',\n",
       "  'faceRectangle': {'top': 69, 'left': 445, 'width': 52, 'height': 52},\n",
       "  'faceAttributes': {'gender': 'female',\n",
       "   'age': 21.0,\n",
       "   'glasses': 'NoGlasses',\n",
       "   'emotion': {'anger': 0.0,\n",
       "    'contempt': 0.0,\n",
       "    'disgust': 0.0,\n",
       "    'fear': 0.0,\n",
       "    'happiness': 1.0,\n",
       "    'neutral': 0.0,\n",
       "    'sadness': 0.0,\n",
       "    'surprise': 0.0}}},\n",
       " {'faceId': '51b31954-6ce0-4d69-8293-6e671bd066ca',\n",
       "  'faceRectangle': {'top': 95, 'left': 238, 'width': 51, 'height': 51},\n",
       "  'faceAttributes': {'gender': 'female',\n",
       "   'age': 17.0,\n",
       "   'glasses': 'ReadingGlasses',\n",
       "   'emotion': {'anger': 0.0,\n",
       "    'contempt': 0.0,\n",
       "    'disgust': 0.0,\n",
       "    'fear': 0.0,\n",
       "    'happiness': 0.981,\n",
       "    'neutral': 0.019,\n",
       "    'sadness': 0.0,\n",
       "    'surprise': 0.0}}},\n",
       " {'faceId': 'fddf73ae-ae07-42e7-9cd9-93f13cf7f92a',\n",
       "  'faceRectangle': {'top': 94, 'left': 540, 'width': 48, 'height': 48},\n",
       "  'faceAttributes': {'gender': 'female',\n",
       "   'age': 19.0,\n",
       "   'glasses': 'NoGlasses',\n",
       "   'emotion': {'anger': 0.0,\n",
       "    'contempt': 0.0,\n",
       "    'disgust': 0.0,\n",
       "    'fear': 0.0,\n",
       "    'happiness': 1.0,\n",
       "    'neutral': 0.0,\n",
       "    'sadness': 0.0,\n",
       "    'surprise': 0.0}}}]"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import requests\n",
    "import json\n",
    "\n",
    "KEY = '99547ad21d8c4c25801a6826ec4b2200'  # Replace with a valid Subscription Key here.\n",
    "\n",
    "# Base URL,  Request URL中 符号?以前\n",
    "BASE_URL = 'https://westcentralus.api.cognitive.microsoft.com/face/v1.0/detect' \n",
    "\n",
    "# 沿用API的示范代碼，{subscription key}用KEY代入\n",
    "HEADERS = {\n",
    "    # Request headers\n",
    "    'Content-Type': 'application/json',\n",
    "    'Ocp-Apim-Subscription-Key': '{}'.format(KEY),\n",
    "}\n",
    "\n",
    "img_url = 'http://newmedia.nfu.edu.cn/wcy/wp-content/uploads/2018/04/post_20180424__NFU_DoraHacks_imoji%E5%9B%A2%E9%98%9F.jpg'\n",
    "\n",
    "data = {\n",
    "    'url': '{}'.format(img_url),\n",
    "}\n",
    "payload = {\n",
    "    'returnFaceId': 'true',\n",
    "    'returnFaceLandmarks': 'flase',\n",
    "    'returnFaceAttributes': '{}'.format('age,gender,glasses,emotion'), \n",
    "}\n",
    "\n",
    "# 坑。参考http://docs.python-requests.org/zh_CN/latest/user/quickstart.html  【更加复杂的post请求】\n",
    "# 差別是 string 字串 vs. dict 字典\n",
    "# Azura 使用的是 data = json.dumps(payload) 或 json=payload，data = payload 会出错\n",
    "r = requests.post(BASE_URL, data=json.dumps(data), params=payload, headers=HEADERS)\n",
    "\n",
    "r.status_code\n",
    "result = r.json()\n",
    "result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>faceAttributes.age</th>\n",
       "      <th>faceAttributes.emotion.anger</th>\n",
       "      <th>faceAttributes.emotion.contempt</th>\n",
       "      <th>faceAttributes.emotion.disgust</th>\n",
       "      <th>faceAttributes.emotion.fear</th>\n",
       "      <th>faceAttributes.emotion.happiness</th>\n",
       "      <th>faceAttributes.emotion.neutral</th>\n",
       "      <th>faceAttributes.emotion.sadness</th>\n",
       "      <th>faceAttributes.emotion.surprise</th>\n",
       "      <th>faceAttributes.gender</th>\n",
       "      <th>faceAttributes.glasses</th>\n",
       "      <th>faceId</th>\n",
       "      <th>faceRectangle.height</th>\n",
       "      <th>faceRectangle.left</th>\n",
       "      <th>faceRectangle.top</th>\n",
       "      <th>faceRectangle.width</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>18.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.003</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.813</td>\n",
       "      <td>0.184</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>male</td>\n",
       "      <td>NoGlasses</td>\n",
       "      <td>feb7e023-50df-4476-a811-5ec7dfaa731f</td>\n",
       "      <td>88</td>\n",
       "      <td>144</td>\n",
       "      <td>118</td>\n",
       "      <td>88</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>20.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.001</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.456</td>\n",
       "      <td>0.542</td>\n",
       "      <td>0.001</td>\n",
       "      <td>0.0</td>\n",
       "      <td>female</td>\n",
       "      <td>NoGlasses</td>\n",
       "      <td>a7ba8baa-b4a3-4349-bd8c-6444e7b1c71e</td>\n",
       "      <td>64</td>\n",
       "      <td>376</td>\n",
       "      <td>117</td>\n",
       "      <td>64</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>23.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>male</td>\n",
       "      <td>ReadingGlasses</td>\n",
       "      <td>6ff3abad-c8ed-4336-ae2c-f222d989c3b5</td>\n",
       "      <td>52</td>\n",
       "      <td>676</td>\n",
       "      <td>41</td>\n",
       "      <td>52</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>21.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>female</td>\n",
       "      <td>NoGlasses</td>\n",
       "      <td>dd6927c2-6766-4bc8-9fe3-b3ee0e42c576</td>\n",
       "      <td>52</td>\n",
       "      <td>445</td>\n",
       "      <td>69</td>\n",
       "      <td>52</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>17.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.981</td>\n",
       "      <td>0.019</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>female</td>\n",
       "      <td>ReadingGlasses</td>\n",
       "      <td>51b31954-6ce0-4d69-8293-6e671bd066ca</td>\n",
       "      <td>51</td>\n",
       "      <td>238</td>\n",
       "      <td>95</td>\n",
       "      <td>51</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>19.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>female</td>\n",
       "      <td>NoGlasses</td>\n",
       "      <td>fddf73ae-ae07-42e7-9cd9-93f13cf7f92a</td>\n",
       "      <td>48</td>\n",
       "      <td>540</td>\n",
       "      <td>94</td>\n",
       "      <td>48</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   faceAttributes.age  faceAttributes.emotion.anger  \\\n",
       "0                18.0                           0.0   \n",
       "1                20.0                           0.0   \n",
       "2                23.0                           0.0   \n",
       "3                21.0                           0.0   \n",
       "4                17.0                           0.0   \n",
       "5                19.0                           0.0   \n",
       "\n",
       "   faceAttributes.emotion.contempt  faceAttributes.emotion.disgust  \\\n",
       "0                            0.003                             0.0   \n",
       "1                            0.001                             0.0   \n",
       "2                            0.000                             0.0   \n",
       "3                            0.000                             0.0   \n",
       "4                            0.000                             0.0   \n",
       "5                            0.000                             0.0   \n",
       "\n",
       "   faceAttributes.emotion.fear  faceAttributes.emotion.happiness  \\\n",
       "0                          0.0                             0.813   \n",
       "1                          0.0                             0.456   \n",
       "2                          0.0                             1.000   \n",
       "3                          0.0                             1.000   \n",
       "4                          0.0                             0.981   \n",
       "5                          0.0                             1.000   \n",
       "\n",
       "   faceAttributes.emotion.neutral  faceAttributes.emotion.sadness  \\\n",
       "0                           0.184                           0.000   \n",
       "1                           0.542                           0.001   \n",
       "2                           0.000                           0.000   \n",
       "3                           0.000                           0.000   \n",
       "4                           0.019                           0.000   \n",
       "5                           0.000                           0.000   \n",
       "\n",
       "   faceAttributes.emotion.surprise faceAttributes.gender  \\\n",
       "0                              0.0                  male   \n",
       "1                              0.0                female   \n",
       "2                              0.0                  male   \n",
       "3                              0.0                female   \n",
       "4                              0.0                female   \n",
       "5                              0.0                female   \n",
       "\n",
       "  faceAttributes.glasses                                faceId  \\\n",
       "0              NoGlasses  feb7e023-50df-4476-a811-5ec7dfaa731f   \n",
       "1              NoGlasses  a7ba8baa-b4a3-4349-bd8c-6444e7b1c71e   \n",
       "2         ReadingGlasses  6ff3abad-c8ed-4336-ae2c-f222d989c3b5   \n",
       "3              NoGlasses  dd6927c2-6766-4bc8-9fe3-b3ee0e42c576   \n",
       "4         ReadingGlasses  51b31954-6ce0-4d69-8293-6e671bd066ca   \n",
       "5              NoGlasses  fddf73ae-ae07-42e7-9cd9-93f13cf7f92a   \n",
       "\n",
       "   faceRectangle.height  faceRectangle.left  faceRectangle.top  \\\n",
       "0                    88                 144                118   \n",
       "1                    64                 376                117   \n",
       "2                    52                 676                 41   \n",
       "3                    52                 445                 69   \n",
       "4                    51                 238                 95   \n",
       "5                    48                 540                 94   \n",
       "\n",
       "   faceRectangle.width  \n",
       "0                   88  \n",
       "1                   64  \n",
       "2                   52  \n",
       "3                   52  \n",
       "4                   51  \n",
       "5                   48  "
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "from pandas.io.json import json_normalize\n",
    "df = pd.io.json.json_normalize(result)\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>恐惧</th>\n",
       "      <th>高兴</th>\n",
       "      <th>平静</th>\n",
       "      <th>伤心</th>\n",
       "      <th>惊讶</th>\n",
       "      <th>性别</th>\n",
       "      <th>眼镜</th>\n",
       "      <th>faceRectangle.height</th>\n",
       "      <th>faceRectangle.left</th>\n",
       "      <th>faceRectangle.top</th>\n",
       "      <th>faceRectangle.width</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>faceId</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>feb7e023-50df-4476-a811-5ec7dfaa731f</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.813</td>\n",
       "      <td>0.184</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>男性</td>\n",
       "      <td>没戴眼睛</td>\n",
       "      <td>88</td>\n",
       "      <td>144</td>\n",
       "      <td>118</td>\n",
       "      <td>88</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>a7ba8baa-b4a3-4349-bd8c-6444e7b1c71e</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.456</td>\n",
       "      <td>0.542</td>\n",
       "      <td>0.001</td>\n",
       "      <td>0.0</td>\n",
       "      <td>女性</td>\n",
       "      <td>没戴眼睛</td>\n",
       "      <td>64</td>\n",
       "      <td>376</td>\n",
       "      <td>117</td>\n",
       "      <td>64</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6ff3abad-c8ed-4336-ae2c-f222d989c3b5</th>\n",
       "      <td>0.0</td>\n",
       "      <td>1.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>男性</td>\n",
       "      <td>戴眼镜</td>\n",
       "      <td>52</td>\n",
       "      <td>676</td>\n",
       "      <td>41</td>\n",
       "      <td>52</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>dd6927c2-6766-4bc8-9fe3-b3ee0e42c576</th>\n",
       "      <td>0.0</td>\n",
       "      <td>1.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>女性</td>\n",
       "      <td>没戴眼睛</td>\n",
       "      <td>52</td>\n",
       "      <td>445</td>\n",
       "      <td>69</td>\n",
       "      <td>52</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>51b31954-6ce0-4d69-8293-6e671bd066ca</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.981</td>\n",
       "      <td>0.019</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>女性</td>\n",
       "      <td>戴眼镜</td>\n",
       "      <td>51</td>\n",
       "      <td>238</td>\n",
       "      <td>95</td>\n",
       "      <td>51</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>fddf73ae-ae07-42e7-9cd9-93f13cf7f92a</th>\n",
       "      <td>0.0</td>\n",
       "      <td>1.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>女性</td>\n",
       "      <td>没戴眼睛</td>\n",
       "      <td>48</td>\n",
       "      <td>540</td>\n",
       "      <td>94</td>\n",
       "      <td>48</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                       恐惧     高兴     平静     伤心   惊讶  性别    眼镜  \\\n",
       "faceId                                                                          \n",
       "feb7e023-50df-4476-a811-5ec7dfaa731f  0.0  0.813  0.184  0.000  0.0  男性  没戴眼睛   \n",
       "a7ba8baa-b4a3-4349-bd8c-6444e7b1c71e  0.0  0.456  0.542  0.001  0.0  女性  没戴眼睛   \n",
       "6ff3abad-c8ed-4336-ae2c-f222d989c3b5  0.0  1.000  0.000  0.000  0.0  男性   戴眼镜   \n",
       "dd6927c2-6766-4bc8-9fe3-b3ee0e42c576  0.0  1.000  0.000  0.000  0.0  女性  没戴眼睛   \n",
       "51b31954-6ce0-4d69-8293-6e671bd066ca  0.0  0.981  0.019  0.000  0.0  女性   戴眼镜   \n",
       "fddf73ae-ae07-42e7-9cd9-93f13cf7f92a  0.0  1.000  0.000  0.000  0.0  女性  没戴眼睛   \n",
       "\n",
       "                                      faceRectangle.height  \\\n",
       "faceId                                                       \n",
       "feb7e023-50df-4476-a811-5ec7dfaa731f                    88   \n",
       "a7ba8baa-b4a3-4349-bd8c-6444e7b1c71e                    64   \n",
       "6ff3abad-c8ed-4336-ae2c-f222d989c3b5                    52   \n",
       "dd6927c2-6766-4bc8-9fe3-b3ee0e42c576                    52   \n",
       "51b31954-6ce0-4d69-8293-6e671bd066ca                    51   \n",
       "fddf73ae-ae07-42e7-9cd9-93f13cf7f92a                    48   \n",
       "\n",
       "                                      faceRectangle.left  faceRectangle.top  \\\n",
       "faceId                                                                        \n",
       "feb7e023-50df-4476-a811-5ec7dfaa731f                 144                118   \n",
       "a7ba8baa-b4a3-4349-bd8c-6444e7b1c71e                 376                117   \n",
       "6ff3abad-c8ed-4336-ae2c-f222d989c3b5                 676                 41   \n",
       "dd6927c2-6766-4bc8-9fe3-b3ee0e42c576                 445                 69   \n",
       "51b31954-6ce0-4d69-8293-6e671bd066ca                 238                 95   \n",
       "fddf73ae-ae07-42e7-9cd9-93f13cf7f92a                 540                 94   \n",
       "\n",
       "                                      faceRectangle.width  \n",
       "faceId                                                     \n",
       "feb7e023-50df-4476-a811-5ec7dfaa731f                   88  \n",
       "a7ba8baa-b4a3-4349-bd8c-6444e7b1c71e                   64  \n",
       "6ff3abad-c8ed-4336-ae2c-f222d989c3b5                   52  \n",
       "dd6927c2-6766-4bc8-9fe3-b3ee0e42c576                   52  \n",
       "51b31954-6ce0-4d69-8293-6e671bd066ca                   51  \n",
       "fddf73ae-ae07-42e7-9cd9-93f13cf7f92a                   48  "
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = df.rename ( columns = {\"faceAttributes.gender\":\"性别\", \n",
    "                       \"faceAttributes.age\":\"年龄\",\n",
    "                       \"faceAttributes.glasses\":\"眼镜\",\n",
    "                       \"faceAttributes.emotion.anger\":\"生气\",\n",
    "                       \"faceAttributes.emotion.contempt\":\"蔑视\",\n",
    "                       \"faceAttributes.emotion.disgust\":\"厌恶\",\n",
    "                       \"faceAttributes.emotion.fear\":\"恐惧\",\n",
    "                       \"faceAttributes.emotion.happiness\":\"高兴\",\n",
    "                       \"faceAttributes.emotion.neutral\":\"平静\",\n",
    "                       \"faceAttributes.emotion.sadness\":\"伤心\",\n",
    "                       \"faceAttributes.emotion.surprise\":\"惊讶\",} )\n",
    "df = df.rename ( columns = {\"faceAttributes.gender\":\"性别\", \n",
    "                       \"faceAttributes.age\":\"年龄\",\n",
    "                       \"faceAttributes.glasses\":\"眼镜\",\n",
    "                       \"faceAttributes.emotion.anger\":\"生气\",\n",
    "                       \"faceAttributes.emotion.contempt\":\"蔑视\",\n",
    "                       \"faceAttributes.emotion.disgust\":\"厌恶\",\n",
    "                       \"faceAttributes.emotion.fear\":\"恐惧\",\n",
    "                       \"faceAttributes.emotion.happiness\":\"高兴\",\n",
    "                       \"faceAttributes.emotion.neutral\":\"平静\",\n",
    "                       \"faceAttributes.emotion.sadness\":\"伤心\",\n",
    "                       \"faceAttributes.emotion.surprise\":\"惊讶\",} )\n",
    "df = df.set_index('faceId')\n",
    "df = df.iloc[:,4:]\n",
    "df.replace({\"male\":\"男性\",\n",
    "               \"female\":\"女性\",\n",
    "              \"NoGlasses\":\"没戴眼睛\",\n",
    "              \"ReadingGlasses\":\"戴眼镜\",})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 将json数据转化为数据框的形式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>faceAttributes</th>\n",
       "      <th>faceId</th>\n",
       "      <th>faceRectangle</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>{'gender': 'male', 'age': 18.0, 'glasses': 'No...</td>\n",
       "      <td>feb7e023-50df-4476-a811-5ec7dfaa731f</td>\n",
       "      <td>{'top': 118, 'left': 144, 'width': 88, 'height...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>{'gender': 'female', 'age': 20.0, 'glasses': '...</td>\n",
       "      <td>a7ba8baa-b4a3-4349-bd8c-6444e7b1c71e</td>\n",
       "      <td>{'top': 117, 'left': 376, 'width': 64, 'height...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>{'gender': 'male', 'age': 23.0, 'glasses': 'Re...</td>\n",
       "      <td>6ff3abad-c8ed-4336-ae2c-f222d989c3b5</td>\n",
       "      <td>{'top': 41, 'left': 676, 'width': 52, 'height'...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>{'gender': 'female', 'age': 21.0, 'glasses': '...</td>\n",
       "      <td>dd6927c2-6766-4bc8-9fe3-b3ee0e42c576</td>\n",
       "      <td>{'top': 69, 'left': 445, 'width': 52, 'height'...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>{'gender': 'female', 'age': 17.0, 'glasses': '...</td>\n",
       "      <td>51b31954-6ce0-4d69-8293-6e671bd066ca</td>\n",
       "      <td>{'top': 95, 'left': 238, 'width': 51, 'height'...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>{'gender': 'female', 'age': 19.0, 'glasses': '...</td>\n",
       "      <td>fddf73ae-ae07-42e7-9cd9-93f13cf7f92a</td>\n",
       "      <td>{'top': 94, 'left': 540, 'width': 48, 'height'...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                      faceAttributes  \\\n",
       "0  {'gender': 'male', 'age': 18.0, 'glasses': 'No...   \n",
       "1  {'gender': 'female', 'age': 20.0, 'glasses': '...   \n",
       "2  {'gender': 'male', 'age': 23.0, 'glasses': 'Re...   \n",
       "3  {'gender': 'female', 'age': 21.0, 'glasses': '...   \n",
       "4  {'gender': 'female', 'age': 17.0, 'glasses': '...   \n",
       "5  {'gender': 'female', 'age': 19.0, 'glasses': '...   \n",
       "\n",
       "                                 faceId  \\\n",
       "0  feb7e023-50df-4476-a811-5ec7dfaa731f   \n",
       "1  a7ba8baa-b4a3-4349-bd8c-6444e7b1c71e   \n",
       "2  6ff3abad-c8ed-4336-ae2c-f222d989c3b5   \n",
       "3  dd6927c2-6766-4bc8-9fe3-b3ee0e42c576   \n",
       "4  51b31954-6ce0-4d69-8293-6e671bd066ca   \n",
       "5  fddf73ae-ae07-42e7-9cd9-93f13cf7f92a   \n",
       "\n",
       "                                       faceRectangle  \n",
       "0  {'top': 118, 'left': 144, 'width': 88, 'height...  \n",
       "1  {'top': 117, 'left': 376, 'width': 64, 'height...  \n",
       "2  {'top': 41, 'left': 676, 'width': 52, 'height'...  \n",
       "3  {'top': 69, 'left': 445, 'width': 52, 'height'...  \n",
       "4  {'top': 95, 'left': 238, 'width': 51, 'height'...  \n",
       "5  {'top': 94, 'left': 540, 'width': 48, 'height'...  "
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "result = pd.DataFrame (result)\n",
    "result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>faceAttributes.age</th>\n",
       "      <th>faceAttributes.emotion.anger</th>\n",
       "      <th>faceAttributes.emotion.contempt</th>\n",
       "      <th>faceAttributes.emotion.disgust</th>\n",
       "      <th>faceAttributes.emotion.fear</th>\n",
       "      <th>faceAttributes.emotion.happiness</th>\n",
       "      <th>faceAttributes.emotion.neutral</th>\n",
       "      <th>faceAttributes.emotion.sadness</th>\n",
       "      <th>faceAttributes.emotion.surprise</th>\n",
       "      <th>faceAttributes.gender</th>\n",
       "      <th>faceAttributes.glasses</th>\n",
       "      <th>faceId</th>\n",
       "      <th>faceRectangle.height</th>\n",
       "      <th>faceRectangle.left</th>\n",
       "      <th>faceRectangle.top</th>\n",
       "      <th>faceRectangle.width</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>18.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.003</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.813</td>\n",
       "      <td>0.184</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>male</td>\n",
       "      <td>NoGlasses</td>\n",
       "      <td>feb7e023-50df-4476-a811-5ec7dfaa731f</td>\n",
       "      <td>88</td>\n",
       "      <td>144</td>\n",
       "      <td>118</td>\n",
       "      <td>88</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>20.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.001</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.456</td>\n",
       "      <td>0.542</td>\n",
       "      <td>0.001</td>\n",
       "      <td>0.0</td>\n",
       "      <td>female</td>\n",
       "      <td>NoGlasses</td>\n",
       "      <td>a7ba8baa-b4a3-4349-bd8c-6444e7b1c71e</td>\n",
       "      <td>64</td>\n",
       "      <td>376</td>\n",
       "      <td>117</td>\n",
       "      <td>64</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>23.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>male</td>\n",
       "      <td>ReadingGlasses</td>\n",
       "      <td>6ff3abad-c8ed-4336-ae2c-f222d989c3b5</td>\n",
       "      <td>52</td>\n",
       "      <td>676</td>\n",
       "      <td>41</td>\n",
       "      <td>52</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>21.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>female</td>\n",
       "      <td>NoGlasses</td>\n",
       "      <td>dd6927c2-6766-4bc8-9fe3-b3ee0e42c576</td>\n",
       "      <td>52</td>\n",
       "      <td>445</td>\n",
       "      <td>69</td>\n",
       "      <td>52</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>17.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.981</td>\n",
       "      <td>0.019</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>female</td>\n",
       "      <td>ReadingGlasses</td>\n",
       "      <td>51b31954-6ce0-4d69-8293-6e671bd066ca</td>\n",
       "      <td>51</td>\n",
       "      <td>238</td>\n",
       "      <td>95</td>\n",
       "      <td>51</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>19.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>female</td>\n",
       "      <td>NoGlasses</td>\n",
       "      <td>fddf73ae-ae07-42e7-9cd9-93f13cf7f92a</td>\n",
       "      <td>48</td>\n",
       "      <td>540</td>\n",
       "      <td>94</td>\n",
       "      <td>48</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   faceAttributes.age  faceAttributes.emotion.anger  \\\n",
       "0                18.0                           0.0   \n",
       "1                20.0                           0.0   \n",
       "2                23.0                           0.0   \n",
       "3                21.0                           0.0   \n",
       "4                17.0                           0.0   \n",
       "5                19.0                           0.0   \n",
       "\n",
       "   faceAttributes.emotion.contempt  faceAttributes.emotion.disgust  \\\n",
       "0                            0.003                             0.0   \n",
       "1                            0.001                             0.0   \n",
       "2                            0.000                             0.0   \n",
       "3                            0.000                             0.0   \n",
       "4                            0.000                             0.0   \n",
       "5                            0.000                             0.0   \n",
       "\n",
       "   faceAttributes.emotion.fear  faceAttributes.emotion.happiness  \\\n",
       "0                          0.0                             0.813   \n",
       "1                          0.0                             0.456   \n",
       "2                          0.0                             1.000   \n",
       "3                          0.0                             1.000   \n",
       "4                          0.0                             0.981   \n",
       "5                          0.0                             1.000   \n",
       "\n",
       "   faceAttributes.emotion.neutral  faceAttributes.emotion.sadness  \\\n",
       "0                           0.184                           0.000   \n",
       "1                           0.542                           0.001   \n",
       "2                           0.000                           0.000   \n",
       "3                           0.000                           0.000   \n",
       "4                           0.019                           0.000   \n",
       "5                           0.000                           0.000   \n",
       "\n",
       "   faceAttributes.emotion.surprise faceAttributes.gender  \\\n",
       "0                              0.0                  male   \n",
       "1                              0.0                female   \n",
       "2                              0.0                  male   \n",
       "3                              0.0                female   \n",
       "4                              0.0                female   \n",
       "5                              0.0                female   \n",
       "\n",
       "  faceAttributes.glasses                                faceId  \\\n",
       "0              NoGlasses  feb7e023-50df-4476-a811-5ec7dfaa731f   \n",
       "1              NoGlasses  a7ba8baa-b4a3-4349-bd8c-6444e7b1c71e   \n",
       "2         ReadingGlasses  6ff3abad-c8ed-4336-ae2c-f222d989c3b5   \n",
       "3              NoGlasses  dd6927c2-6766-4bc8-9fe3-b3ee0e42c576   \n",
       "4         ReadingGlasses  51b31954-6ce0-4d69-8293-6e671bd066ca   \n",
       "5              NoGlasses  fddf73ae-ae07-42e7-9cd9-93f13cf7f92a   \n",
       "\n",
       "   faceRectangle.height  faceRectangle.left  faceRectangle.top  \\\n",
       "0                    88                 144                118   \n",
       "1                    64                 376                117   \n",
       "2                    52                 676                 41   \n",
       "3                    52                 445                 69   \n",
       "4                    51                 238                 95   \n",
       "5                    48                 540                 94   \n",
       "\n",
       "   faceRectangle.width  \n",
       "0                   88  \n",
       "1                   64  \n",
       "2                   52  \n",
       "3                   52  \n",
       "4                   51  \n",
       "5                   48  "
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "from pandas.io.json import json_normalize\n",
    "df = pd.io.json.json_normalize(result)\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 运用face++ API识别图片人脸数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b'{\"request_id\":\"1585818469,bfbc62c1-d7bc-4d0c-8d68-37988f36e1e2\",\"time_used\":391,\"faces\":[{\"face_token\":\"7577b1c42143c0068142495b8af9c5fb\",\"face_rectangle\":{\"top\":123,\"left\":138,\"width\":90,\"height\":90},\"attributes\":{\"gender\":{\"value\":\"Male\"},\"age\":{\"value\":23},\"smile\":{\"value\":99.995,\"threshold\":50.000},\"emotion\":{\"anger\":0.059,\"disgust\":0.059,\"fear\":0.059,\"happiness\":42.294,\"neutral\":52.431,\"sadness\":4.920,\"surprise\":0.179}}},{\"face_token\":\"4e104a88a6e9e8b253fb288108086e09\",\"face_rectangle\":{\"top\":122,\"left\":373,\"width\":65,\"height\":65},\"attributes\":{\"gender\":{\"value\":\"Female\"},\"age\":{\"value\":37},\"smile\":{\"value\":98.866,\"threshold\":50.000},\"emotion\":{\"anger\":0.169,\"disgust\":7.280,\"fear\":0.138,\"happiness\":17.689,\"neutral\":8.067,\"sadness\":66.519,\"surprise\":0.138}}},{\"face_token\":\"68f81d2639bbefbd5d0f11bba9aa7a0a\",\"face_rectangle\":{\"top\":46,\"left\":675,\"width\":54,\"height\":54},\"attributes\":{\"gender\":{\"value\":\"Male\"},\"age\":{\"value\":23},\"smile\":{\"value\":100.000,\"threshold\":50.000},\"emotion\":{\"anger\":0.000,\"disgust\":0.000,\"fear\":0.000,\"happiness\":99.996,\"neutral\":0.001,\"sadness\":0.002,\"surprise\":0.001}}},{\"face_token\":\"2d16a119ab5ecbb3ab1befe96ef74146\",\"face_rectangle\":{\"top\":98,\"left\":235,\"width\":53,\"height\":53},\"attributes\":{\"gender\":{\"value\":\"Female\"},\"age\":{\"value\":22},\"smile\":{\"value\":99.997,\"threshold\":50.000},\"emotion\":{\"anger\":0.000,\"disgust\":0.001,\"fear\":0.000,\"happiness\":99.990,\"neutral\":0.009,\"sadness\":0.000,\"surprise\":0.000}}},{\"face_token\":\"601766467e947064777238abf04f9c5c\",\"face_rectangle\":{\"top\":75,\"left\":444,\"width\":52,\"height\":52},\"attributes\":{\"gender\":{\"value\":\"Female\"},\"age\":{\"value\":28},\"smile\":{\"value\":99.999,\"threshold\":50.000},\"emotion\":{\"anger\":0.001,\"disgust\":0.001,\"fear\":0.027,\"happiness\":99.933,\"neutral\":0.027,\"sadness\":0.010,\"surprise\":0.001}}},{\"face_token\":\"39d622ddc0ae99ac9000ec55cffb4bf8\",\"face_rectangle\":{\"top\":98,\"left\":537,\"width\":48,\"height\":48}}],\"image_id\":\"TfC1AByqtXhfuQ6DruqVww==\",\"face_num\":6}\\n'\n"
     ]
    }
   ],
   "source": [
    "# 1、先导入为们需要的模块\n",
    "import requests\n",
    "\n",
    "\n",
    "api_secret = \"Mg-S0entZbf1iVCBFAgM__d2TOeakTNZ\"\n",
    "# 2、输入我们API_Key\n",
    "api_key = 'aenENjllcy4-fqzCka_HlrunD9EEAy1u'  # Replace with a valid Subscription Key here.\n",
    "\n",
    "\n",
    "# 3、目标url\n",
    "# 这里也可以使用本地图片 例如：filepath =\"image/tupian.jpg\"\n",
    "BASE_URL = 'https://api-cn.faceplusplus.com/facepp/v3/detect' \n",
    "img_url = 'http://newmedia.nfu.edu.cn/wcy/wp-content/uploads/2018/04/post_20180424__NFU_DoraHacks_imoji%E5%9B%A2%E9%98%9F.jpg'\n",
    "\n",
    "# 4、沿用API文档的示范代码,准备我们的headers和图片(数据)\n",
    "\n",
    "headers = {\n",
    "    'Content-Type': 'application/json',\n",
    "}\n",
    "\n",
    "# 5、准备symbol ? 后面的数据\n",
    "\n",
    "payload = {\n",
    "    \"image_url\":img_url,\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'return_attributes':'gender,age,smiling,emotion', \n",
    "}\n",
    "\n",
    "#  6、requests发送我们请求\n",
    "r = requests.post(BASE_URL, params=payload, headers=headers)\n",
    "\n",
    "r.status_code\n",
    "print(r.content)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1585792940,5efaa4a6-37c1-48d0-b288-f4f5d478bd54',\n",
       " 'time_used': 352,\n",
       " 'faces': [{'face_token': '6fe74c76ca218fd127c5ea0d822aa928',\n",
       "   'face_rectangle': {'top': 123, 'left': 138, 'width': 90, 'height': 90},\n",
       "   'attributes': {'gender': {'value': 'Male'},\n",
       "    'age': {'value': 23},\n",
       "    'smile': {'value': 99.995, 'threshold': 50.0},\n",
       "    'emotion': {'anger': 0.059,\n",
       "     'disgust': 0.059,\n",
       "     'fear': 0.059,\n",
       "     'happiness': 42.294,\n",
       "     'neutral': 52.431,\n",
       "     'sadness': 4.92,\n",
       "     'surprise': 0.179}}},\n",
       "  {'face_token': '59a071e1bf3a2b467a5fabdff03f87bb',\n",
       "   'face_rectangle': {'top': 122, 'left': 373, 'width': 65, 'height': 65},\n",
       "   'attributes': {'gender': {'value': 'Female'},\n",
       "    'age': {'value': 37},\n",
       "    'smile': {'value': 98.866, 'threshold': 50.0},\n",
       "    'emotion': {'anger': 0.169,\n",
       "     'disgust': 7.28,\n",
       "     'fear': 0.138,\n",
       "     'happiness': 17.689,\n",
       "     'neutral': 8.067,\n",
       "     'sadness': 66.519,\n",
       "     'surprise': 0.138}}},\n",
       "  {'face_token': 'b1eecd92f8e2b6390ec3c5cb9a3622bf',\n",
       "   'face_rectangle': {'top': 46, 'left': 675, 'width': 54, 'height': 54},\n",
       "   'attributes': {'gender': {'value': 'Male'},\n",
       "    'age': {'value': 23},\n",
       "    'smile': {'value': 100.0, 'threshold': 50.0},\n",
       "    'emotion': {'anger': 0.0,\n",
       "     'disgust': 0.0,\n",
       "     'fear': 0.0,\n",
       "     'happiness': 99.996,\n",
       "     'neutral': 0.001,\n",
       "     'sadness': 0.002,\n",
       "     'surprise': 0.001}}},\n",
       "  {'face_token': 'c638eea7d62d884b5a1f07e46c8eed91',\n",
       "   'face_rectangle': {'top': 98, 'left': 235, 'width': 53, 'height': 53},\n",
       "   'attributes': {'gender': {'value': 'Female'},\n",
       "    'age': {'value': 22},\n",
       "    'smile': {'value': 99.997, 'threshold': 50.0},\n",
       "    'emotion': {'anger': 0.0,\n",
       "     'disgust': 0.001,\n",
       "     'fear': 0.0,\n",
       "     'happiness': 99.99,\n",
       "     'neutral': 0.009,\n",
       "     'sadness': 0.0,\n",
       "     'surprise': 0.0}}},\n",
       "  {'face_token': 'f988afb2146da8136661bb032a9f015d',\n",
       "   'face_rectangle': {'top': 75, 'left': 444, 'width': 52, 'height': 52},\n",
       "   'attributes': {'gender': {'value': 'Female'},\n",
       "    'age': {'value': 28},\n",
       "    'smile': {'value': 99.999, 'threshold': 50.0},\n",
       "    'emotion': {'anger': 0.001,\n",
       "     'disgust': 0.001,\n",
       "     'fear': 0.027,\n",
       "     'happiness': 99.933,\n",
       "     'neutral': 0.027,\n",
       "     'sadness': 0.01,\n",
       "     'surprise': 0.001}}},\n",
       "  {'face_token': 'd8df675a699747466fd5b908df2edb24',\n",
       "   'face_rectangle': {'top': 98, 'left': 537, 'width': 48, 'height': 48}}],\n",
       " 'image_id': 'TfC1AByqtXhfuQ6DruqVww==',\n",
       " 'face_num': 6}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "results = r.json()\n",
    "results"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 将json数据转化为数据框的形式呈现"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>faceAttributes.age</th>\n",
       "      <th>faceAttributes.emotion.anger</th>\n",
       "      <th>faceAttributes.emotion.contempt</th>\n",
       "      <th>faceAttributes.emotion.disgust</th>\n",
       "      <th>faceAttributes.emotion.fear</th>\n",
       "      <th>faceAttributes.emotion.happiness</th>\n",
       "      <th>faceAttributes.emotion.neutral</th>\n",
       "      <th>faceAttributes.emotion.sadness</th>\n",
       "      <th>faceAttributes.emotion.surprise</th>\n",
       "      <th>faceAttributes.gender</th>\n",
       "      <th>faceAttributes.glasses</th>\n",
       "      <th>faceId</th>\n",
       "      <th>faceRectangle.height</th>\n",
       "      <th>faceRectangle.left</th>\n",
       "      <th>faceRectangle.top</th>\n",
       "      <th>faceRectangle.width</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>18.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.003</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.813</td>\n",
       "      <td>0.184</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>male</td>\n",
       "      <td>NoGlasses</td>\n",
       "      <td>feb7e023-50df-4476-a811-5ec7dfaa731f</td>\n",
       "      <td>88</td>\n",
       "      <td>144</td>\n",
       "      <td>118</td>\n",
       "      <td>88</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>20.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.001</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.456</td>\n",
       "      <td>0.542</td>\n",
       "      <td>0.001</td>\n",
       "      <td>0.0</td>\n",
       "      <td>female</td>\n",
       "      <td>NoGlasses</td>\n",
       "      <td>a7ba8baa-b4a3-4349-bd8c-6444e7b1c71e</td>\n",
       "      <td>64</td>\n",
       "      <td>376</td>\n",
       "      <td>117</td>\n",
       "      <td>64</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>23.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>male</td>\n",
       "      <td>ReadingGlasses</td>\n",
       "      <td>6ff3abad-c8ed-4336-ae2c-f222d989c3b5</td>\n",
       "      <td>52</td>\n",
       "      <td>676</td>\n",
       "      <td>41</td>\n",
       "      <td>52</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>21.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>female</td>\n",
       "      <td>NoGlasses</td>\n",
       "      <td>dd6927c2-6766-4bc8-9fe3-b3ee0e42c576</td>\n",
       "      <td>52</td>\n",
       "      <td>445</td>\n",
       "      <td>69</td>\n",
       "      <td>52</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>17.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.981</td>\n",
       "      <td>0.019</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>female</td>\n",
       "      <td>ReadingGlasses</td>\n",
       "      <td>51b31954-6ce0-4d69-8293-6e671bd066ca</td>\n",
       "      <td>51</td>\n",
       "      <td>238</td>\n",
       "      <td>95</td>\n",
       "      <td>51</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>19.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>female</td>\n",
       "      <td>NoGlasses</td>\n",
       "      <td>fddf73ae-ae07-42e7-9cd9-93f13cf7f92a</td>\n",
       "      <td>48</td>\n",
       "      <td>540</td>\n",
       "      <td>94</td>\n",
       "      <td>48</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   faceAttributes.age  faceAttributes.emotion.anger  \\\n",
       "0                18.0                           0.0   \n",
       "1                20.0                           0.0   \n",
       "2                23.0                           0.0   \n",
       "3                21.0                           0.0   \n",
       "4                17.0                           0.0   \n",
       "5                19.0                           0.0   \n",
       "\n",
       "   faceAttributes.emotion.contempt  faceAttributes.emotion.disgust  \\\n",
       "0                            0.003                             0.0   \n",
       "1                            0.001                             0.0   \n",
       "2                            0.000                             0.0   \n",
       "3                            0.000                             0.0   \n",
       "4                            0.000                             0.0   \n",
       "5                            0.000                             0.0   \n",
       "\n",
       "   faceAttributes.emotion.fear  faceAttributes.emotion.happiness  \\\n",
       "0                          0.0                             0.813   \n",
       "1                          0.0                             0.456   \n",
       "2                          0.0                             1.000   \n",
       "3                          0.0                             1.000   \n",
       "4                          0.0                             0.981   \n",
       "5                          0.0                             1.000   \n",
       "\n",
       "   faceAttributes.emotion.neutral  faceAttributes.emotion.sadness  \\\n",
       "0                           0.184                           0.000   \n",
       "1                           0.542                           0.001   \n",
       "2                           0.000                           0.000   \n",
       "3                           0.000                           0.000   \n",
       "4                           0.019                           0.000   \n",
       "5                           0.000                           0.000   \n",
       "\n",
       "   faceAttributes.emotion.surprise faceAttributes.gender  \\\n",
       "0                              0.0                  male   \n",
       "1                              0.0                female   \n",
       "2                              0.0                  male   \n",
       "3                              0.0                female   \n",
       "4                              0.0                female   \n",
       "5                              0.0                female   \n",
       "\n",
       "  faceAttributes.glasses                                faceId  \\\n",
       "0              NoGlasses  feb7e023-50df-4476-a811-5ec7dfaa731f   \n",
       "1              NoGlasses  a7ba8baa-b4a3-4349-bd8c-6444e7b1c71e   \n",
       "2         ReadingGlasses  6ff3abad-c8ed-4336-ae2c-f222d989c3b5   \n",
       "3              NoGlasses  dd6927c2-6766-4bc8-9fe3-b3ee0e42c576   \n",
       "4         ReadingGlasses  51b31954-6ce0-4d69-8293-6e671bd066ca   \n",
       "5              NoGlasses  fddf73ae-ae07-42e7-9cd9-93f13cf7f92a   \n",
       "\n",
       "   faceRectangle.height  faceRectangle.left  faceRectangle.top  \\\n",
       "0                    88                 144                118   \n",
       "1                    64                 376                117   \n",
       "2                    52                 676                 41   \n",
       "3                    52                 445                 69   \n",
       "4                    51                 238                 95   \n",
       "5                    48                 540                 94   \n",
       "\n",
       "   faceRectangle.width  \n",
       "0                   88  \n",
       "1                   64  \n",
       "2                   52  \n",
       "3                   52  \n",
       "4                   51  \n",
       "5                   48  "
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "from pandas.io.json import json_normalize\n",
    "df = pd.io.json.json_normalize(result)\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>恐惧</th>\n",
       "      <th>高兴</th>\n",
       "      <th>平静</th>\n",
       "      <th>伤心</th>\n",
       "      <th>惊讶</th>\n",
       "      <th>性别</th>\n",
       "      <th>眼镜</th>\n",
       "      <th>faceRectangle.height</th>\n",
       "      <th>faceRectangle.left</th>\n",
       "      <th>faceRectangle.top</th>\n",
       "      <th>faceRectangle.width</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>faceId</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>feb7e023-50df-4476-a811-5ec7dfaa731f</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.813</td>\n",
       "      <td>0.184</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>男性</td>\n",
       "      <td>没戴眼睛</td>\n",
       "      <td>88</td>\n",
       "      <td>144</td>\n",
       "      <td>118</td>\n",
       "      <td>88</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>a7ba8baa-b4a3-4349-bd8c-6444e7b1c71e</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.456</td>\n",
       "      <td>0.542</td>\n",
       "      <td>0.001</td>\n",
       "      <td>0.0</td>\n",
       "      <td>女性</td>\n",
       "      <td>没戴眼睛</td>\n",
       "      <td>64</td>\n",
       "      <td>376</td>\n",
       "      <td>117</td>\n",
       "      <td>64</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6ff3abad-c8ed-4336-ae2c-f222d989c3b5</th>\n",
       "      <td>0.0</td>\n",
       "      <td>1.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>男性</td>\n",
       "      <td>戴眼镜</td>\n",
       "      <td>52</td>\n",
       "      <td>676</td>\n",
       "      <td>41</td>\n",
       "      <td>52</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>dd6927c2-6766-4bc8-9fe3-b3ee0e42c576</th>\n",
       "      <td>0.0</td>\n",
       "      <td>1.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>女性</td>\n",
       "      <td>没戴眼睛</td>\n",
       "      <td>52</td>\n",
       "      <td>445</td>\n",
       "      <td>69</td>\n",
       "      <td>52</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>51b31954-6ce0-4d69-8293-6e671bd066ca</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.981</td>\n",
       "      <td>0.019</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>女性</td>\n",
       "      <td>戴眼镜</td>\n",
       "      <td>51</td>\n",
       "      <td>238</td>\n",
       "      <td>95</td>\n",
       "      <td>51</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>fddf73ae-ae07-42e7-9cd9-93f13cf7f92a</th>\n",
       "      <td>0.0</td>\n",
       "      <td>1.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>女性</td>\n",
       "      <td>没戴眼睛</td>\n",
       "      <td>48</td>\n",
       "      <td>540</td>\n",
       "      <td>94</td>\n",
       "      <td>48</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                       恐惧     高兴     平静     伤心   惊讶  性别    眼镜  \\\n",
       "faceId                                                                          \n",
       "feb7e023-50df-4476-a811-5ec7dfaa731f  0.0  0.813  0.184  0.000  0.0  男性  没戴眼睛   \n",
       "a7ba8baa-b4a3-4349-bd8c-6444e7b1c71e  0.0  0.456  0.542  0.001  0.0  女性  没戴眼睛   \n",
       "6ff3abad-c8ed-4336-ae2c-f222d989c3b5  0.0  1.000  0.000  0.000  0.0  男性   戴眼镜   \n",
       "dd6927c2-6766-4bc8-9fe3-b3ee0e42c576  0.0  1.000  0.000  0.000  0.0  女性  没戴眼睛   \n",
       "51b31954-6ce0-4d69-8293-6e671bd066ca  0.0  0.981  0.019  0.000  0.0  女性   戴眼镜   \n",
       "fddf73ae-ae07-42e7-9cd9-93f13cf7f92a  0.0  1.000  0.000  0.000  0.0  女性  没戴眼睛   \n",
       "\n",
       "                                      faceRectangle.height  \\\n",
       "faceId                                                       \n",
       "feb7e023-50df-4476-a811-5ec7dfaa731f                    88   \n",
       "a7ba8baa-b4a3-4349-bd8c-6444e7b1c71e                    64   \n",
       "6ff3abad-c8ed-4336-ae2c-f222d989c3b5                    52   \n",
       "dd6927c2-6766-4bc8-9fe3-b3ee0e42c576                    52   \n",
       "51b31954-6ce0-4d69-8293-6e671bd066ca                    51   \n",
       "fddf73ae-ae07-42e7-9cd9-93f13cf7f92a                    48   \n",
       "\n",
       "                                      faceRectangle.left  faceRectangle.top  \\\n",
       "faceId                                                                        \n",
       "feb7e023-50df-4476-a811-5ec7dfaa731f                 144                118   \n",
       "a7ba8baa-b4a3-4349-bd8c-6444e7b1c71e                 376                117   \n",
       "6ff3abad-c8ed-4336-ae2c-f222d989c3b5                 676                 41   \n",
       "dd6927c2-6766-4bc8-9fe3-b3ee0e42c576                 445                 69   \n",
       "51b31954-6ce0-4d69-8293-6e671bd066ca                 238                 95   \n",
       "fddf73ae-ae07-42e7-9cd9-93f13cf7f92a                 540                 94   \n",
       "\n",
       "                                      faceRectangle.width  \n",
       "faceId                                                     \n",
       "feb7e023-50df-4476-a811-5ec7dfaa731f                   88  \n",
       "a7ba8baa-b4a3-4349-bd8c-6444e7b1c71e                   64  \n",
       "6ff3abad-c8ed-4336-ae2c-f222d989c3b5                   52  \n",
       "dd6927c2-6766-4bc8-9fe3-b3ee0e42c576                   52  \n",
       "51b31954-6ce0-4d69-8293-6e671bd066ca                   51  \n",
       "fddf73ae-ae07-42e7-9cd9-93f13cf7f92a                   48  "
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = df.rename ( columns = {\"faceAttributes.gender\":\"性别\", \n",
    "                       \"faceAttributes.age\":\"年龄\",\n",
    "                       \"faceAttributes.glasses\":\"眼镜\",\n",
    "                       \"faceAttributes.emotion.anger\":\"生气\",\n",
    "                       \"faceAttributes.emotion.contempt\":\"蔑视\",\n",
    "                       \"faceAttributes.emotion.disgust\":\"厌恶\",\n",
    "                       \"faceAttributes.emotion.fear\":\"恐惧\",\n",
    "                       \"faceAttributes.emotion.happiness\":\"高兴\",\n",
    "                       \"faceAttributes.emotion.neutral\":\"平静\",\n",
    "                       \"faceAttributes.emotion.sadness\":\"伤心\",\n",
    "                       \"faceAttributes.emotion.surprise\":\"惊讶\",} )\n",
    "df = df.rename ( columns = {\"faceAttributes.gender\":\"性别\", \n",
    "                       \"faceAttributes.age\":\"年龄\",\n",
    "                       \"faceAttributes.glasses\":\"眼镜\",\n",
    "                       \"faceAttributes.emotion.anger\":\"生气\",\n",
    "                       \"faceAttributes.emotion.contempt\":\"蔑视\",\n",
    "                       \"faceAttributes.emotion.disgust\":\"厌恶\",\n",
    "                       \"faceAttributes.emotion.fear\":\"恐惧\",\n",
    "                       \"faceAttributes.emotion.happiness\":\"高兴\",\n",
    "                       \"faceAttributes.emotion.neutral\":\"平静\",\n",
    "                       \"faceAttributes.emotion.sadness\":\"伤心\",\n",
    "                       \"faceAttributes.emotion.surprise\":\"惊讶\",} )\n",
    "df = df.set_index('faceId')\n",
    "df = df.iloc[:,4:]\n",
    "df.replace({\"male\":\"男性\",\n",
    "               \"female\":\"女性\",\n",
    "              \"NoGlasses\":\"没戴眼睛\",\n",
    "              \"ReadingGlasses\":\"戴眼镜\",})"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
